/*
 * @Author: zhouwei wei.zhou@flip.fun
 * @Date: 2024-04-07 23:35:15
 * @LastEditors: zhouwei wei.zhou@flip.fun
 * @LastEditTime: 2024-04-08 11:10:38
 * @FilePath: /50/手撕系列/call.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
function bind(obj, ...args) {
	if (typeof this !== 'function') {
		return new Error('Call must be a function')
	}
	const ctx = obj || window
	const fn = this
	const argsArray = args
	return function resultFn(...args) {
		/**
		 * 需要处理当前返回的函数被作为构造函数来使用
		 */
		fn.apply(this instanceof resultFn ? this : ctx, argsArray.concat(args))
	}
}
Function.prototype.myBind = bind
function setAge(age) {
	this.age = age
}
const obj = {
	age: 22,
}
setAge.myBind(obj, 555)()
console.log(obj)
